<template>
  <div style="margin-left:10px">
    <strong style="display: inline-block; margin: 0 0 20px; ">留言管理</strong>
    <!--留言列表 【删除】-->
    <el-table :data="messageList" style="width: 100%">
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="title" label="所属自行车"></el-table-column>
      <el-table-column prop="bikeId" label="所属自行车Id"></el-table-column>
      <el-table-column prop="content" label="留言内容" width="350px"></el-table-column>
      <el-table-column prop="createdAt" label="留言时间"></el-table-column>
      <el-table-column prop="action" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-delete" circle @click="deleteMeg(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getAllMessages, delMessage } from "../../../api";
import { formatTime } from "../../../common/js/myFun";
export default {
  name: "messageManage",
  data() {
    return {
      messageList: []
    };
  },
  mounted() {
    this.getAllMessages();
  },
  methods: {
    getAllMessages() {
      getAllMessages().then(res => {
        if (res.code === 200) {
          let listArr = [];
          res.result.messageList.forEach(item => {
            item.messageInfo.title = item.bikeInfo.title;
            item.messageInfo.createdAt = formatTime(item.messageInfo.createdAt);
            listArr.push(item.messageInfo);
          });
          this.messageList = listArr;
        } else {
          this.$message({
            message: res.data,
            type: "error"
          });
        }
      });
    },
    deleteMeg(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delMessage({
            messageId: id
          }).then(res => {
            if (res.code === 200) {
              this.$message({
                message: res.data,
                type: "success"
              });
              this.getAllMessages()
            } else {
              this.$message({
                message: res.data,
                type: "错误"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped lang="stylus" ref="stylesheet/stylus"></style>
